package com.lxk.service.impl;

import com.lxk.dao.AsMyMapper;
import com.lxk.dao.MomentComMapper;
import com.lxk.dao.MomentMapper;
import com.lxk.pojo.Moment;
import com.lxk.pojo.MomentComExample;
import com.lxk.pojo.MomentExample;
import com.lxk.service.MomentService;
import com.lxk.vo.MomentComShowModel;
import com.lxk.vo.MomentModel;
import com.lxk.vo.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/3 16:36
 * @description
 */
@Service
@Transactional
public class MomentServiceImpl implements MomentService {

    @Autowired
    private MomentMapper mom;
    @Autowired
    private MomentComMapper mcm;
    @Autowired
    private AsMyMapper mm;

    public Integer getUncheckedTotalPages(Integer num) {
        MomentExample example = new MomentExample();
        MomentExample.Criteria criteria = example.createCriteria();
        criteria.andIsPassedEqualTo(0);
        Integer count = (int)mom.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<Moment> selUncheckedMomentPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<Moment> list = mm.selUncheckedMomentPage(start, num, sort, order);
        PageModel<Moment> model = new PageModel<Moment>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Integer getTotalPages(Integer num) {
        MomentExample example = new MomentExample();
        MomentExample.Criteria criteria = example.createCriteria();
        criteria.andIsPassedEqualTo(1);
        Integer count = (int)mom.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<MomentModel> selMomentPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<MomentModel> list = mm.selMomentPage(start, num, sort, order);
        PageModel<MomentModel> model = new PageModel<MomentModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public void checkMoment(Integer momentId, Integer code) {
        Moment moment = new Moment();
        moment.setMomentId(momentId);
        moment.setIsPassed(code);
        mom.updateByPrimaryKeySelective(moment);
    }

    public Moment selMomentById(Integer momentId) {
        Moment moment = mom.selectByPrimaryKey(momentId);
        return moment;
    }

    public void updMoment(Moment moment) {
        mom.updateByPrimaryKeySelective(moment);
    }

    public Integer getComTotalPages(Integer num, Integer momentId) {
        MomentComExample example = new MomentComExample();
        MomentComExample.Criteria criteria = example.createCriteria();
        criteria.andMomentIdEqualTo(momentId);
        int count = (int) mcm.countByExample(example);
        int totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<MomentComShowModel> getComList(Integer pageIndex, Integer num, Integer totalPages, Integer momentId) {
        int start = (pageIndex-1)*num;
        List<MomentComShowModel> list = mm.selMomentComShowPage(start, num, momentId);
        PageModel<MomentComShowModel> model = new PageModel<MomentComShowModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public void deleteMomentComment(Integer momentComId) {
        mcm.deleteByPrimaryKey(momentComId);
    }


}
